;
(function() {
  new Vue({
    el: '#template-list',
    data: {
      stateBtn: [ // 审核状态
        {
          'state': '账单打印模版',
        },
        {
          'state': '合同打印模版',
        },
      ],
      selectedState: '账单打印模版',
      listData: [],
      quanxian: JSON.parse(window.localStorage.getItem('permissions')),
      defaultName: hlUrlUtil.GetQueryString('defaultName'),
    },
    filters: {
      formatDate: function(time) {
        if (time === undefined) return '--'
        return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd')
      },
      auditState: function(item) {
        if (item === '全部') return '#'
        return hlCodeConvert.contractAuditState(item)
      },
      formatUndefined: function(item) {
        if (!item) return '--'
        return item
      },
      formatNum: function(item) {
        if (item === undefined) return '--'
        return hlNumberUtil.formatNum(item)
      }
    },
    methods: {
      //预览
      preview(id) {
        hlhttp.get({
          baseUrl: exclUrl,
          url: 'resource/contract_tempalte_preview',
          data: {
            template_id: id
          },
          success: function(data) {
            $.previewPDF(data.pdf_url)
          }
        })
      },

      // 选择打印类型
      selectType(val) {
        this.selectedState = val
        if (val == '账单打印模版') {
          this.getData();
        } else {
          this.getbillList();
        }
      },
      getData() {
        var self = this;
        hlhttp.get({
          url: 'print_template_list',
          success: function(data) {
            self.listData = data;
          }
        })
      },
      getbillList() {
        var self = this;
        hlhttp.get({
          url: 'contract/print_templates',
          success: function(data) {
            self.listData = data;
          }
        })
      },
      addTmp() {
        if (this.selectedState == '账单打印模版') {
          hlUrlUtil.goTo('templateAdd', {
            ddtab: true
          }, true)
        } else {
          hlUrlUtil.goTo('billTemplate', {
            ddtab: true
          }, true)
        }
      },
      //停用/启用
      changeValid(id, valid) {
        var self = this;
        if (self.selectedState == '账单打印模版') {
          hlhttp.put({
            url: 'bill/print_template/_changeValidState',
            data: {
              'template_id': id,
              'valid': valid
            },
            success: function(data) {
              hl.toast('状态更改成功')
              self.getData()
            }
          })
        } else {
          hlhttp.put({
            url: 'contract/template/_valid',
            data: {
              'template_id': id,
              'opt': valid
            },
            success: function(data) {
              hl.toast('状态更改成功')
              self.getbillList()
            }
          })
        }

      },
      deleteTmp(id) {
        var self = this;
        hllayer.confirm1('此操作不可逆转，确认删除模版吗？', function() {
          if (self.selectedState == '账单打印模版') {
            hlhttp.delete({
              url: 'bill/print_template?template_id=' + id,
              data: {
                'template_id': id
              },
              success: function(data) {
                hl.toast('成功删除模版')
                self.getData()
              }
            })
          } else {
            hlhttp.delete({
              url: 'contract/template?template_id=' + id,
              data: {
                'template_id': id
              },
              success: function(data) {
                hl.toast('成功删除模版')
                self.getbillList()
              }
            })
          }

        })
      }
    },
    mounted: function() {
      if (this.defaultName == 'bill') {
        this.getbillList();
        this.selectedState = '合同打印模版'
      } else {
        this.getData()
      }

    }
  })
}())
